using System;
using System.Diagnostics;
using BitMango;
using BitMango.Diagnostics;
using BitMango.Promotion;

using UnityEngine;

public class AdManager : MonoBehaviour
{
    //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public event Action<AdManager.AdType> OnPrevShow;

    //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public event Action<AdManager.AdType> OnShown;

    //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public event Action<AdManager.AdType> OnClosed;

    public bool Running { get; private set; }

    public static Configuration Config { get; private set; }

    public int ImpressionCount
    {
        get
        {
            return this.impressionCount;
        }
        private set
        {
            this.impressionCount = value;
        }
    }

    public bool VideoAdShown
    {
        get
        {
            return this.videoAdShown;
        }
        set
        {
            this.videoAdShown = value;
        }
    }

    public bool InterstitialAdShown
    {
        get
        {
            return this.interAdShown;
        }
        set
        {
            this.interAdShown = value;
        }
    }

    private float LastDisplayTime
    {
        get
        {
            return this.lastDisplayTime;
        }
        set
        {
            BitMango.Diagnostics.Debugger.Log("AdManager.LastDisplayTime : " + value);
            this.lastDisplayTime = value;
        }
    }


    private void Awake()
    {
        Context.AdManager = this;
        AdManager.Config = Configuration.GetConfig();
        if (AdManager.Config == null)
        {
            BitMango.Diagnostics.Debugger.Assert(false, "AdManager.Awake configuration is null");
            return;
        }
        this.CreateAdsInstance();
        this.CreateBannerBG();
        if (this.useVideo)
        {
            this.RunVideoAds();
        }
    }


    private void CreateAdsInstance()
    {
        foreach (Order order in AdManager.Config.order)
        {
            if (!order.primaryKey.Equals("none", StringComparison.OrdinalIgnoreCase) && !(order.primaryKey == string.Empty))
            {
                AdManager.AdType adType = order.adType;
                if (adType != AdManager.AdType.BANNER)
                {
                    if (adType != AdManager.AdType.INTERSTITIAL)
                    {
                        if (adType == AdManager.AdType.VIDEO)
                        {
                            this.useVideo = true;
                        }
                    }
                    else
                    {
                        this.useInterstitial = true;
                    }
                }
                else
                {
                    this.useBanner = true;
                }
            }
        }
    }

    public void Run(int m = 273)
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.Run");
        if (Profile.IsBigMouth)
        {
            BitMango.Diagnostics.Debugger.Log("AdManager.Run - Stop because user is BigMouth.");
            return;
        }
        if (this.Running)
        {
            BitMango.Diagnostics.Debugger.Log("AdManager.Run Already Running!!");
            return;
        }
        this.Running = true;
        BitMango.Diagnostics.Debugger.Assert(AdManager.Config != null, "AdManager.Run configuration is null");
        this.mode = m;
        BitMango.Diagnostics.Debugger.Log("AdManager.Run - Mode : " + this.mode + " Ads Type");
        if (this.mode != 273)
        {
            this.useVideo = ((this.mode & 256) != 0);
            this.useInterstitial = ((this.mode & 16) != 0);
            this.useBanner = ((this.mode & 1) != 0);
        }
        BitMango.Diagnostics.Debugger.Log(string.Concat(new object[]
        {
            "AdManager.Run use Video : ",
            this.useVideo,
            ", Interstiial : ",
            this.useInterstitial,
            ", Banner : ",
            this.useBanner
        }));
    }

    public void Stop()
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.Stop");
        if (!this.Running)
        {
            BitMango.Diagnostics.Debugger.LogWarning("AdManager.Stop AdManager is not running..");
            return;
        }
        this.Running = false;
    }

    public void SetAdsMuted(bool muted)
    {
    }

    private void RunBannerAds()
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.RunBannerAds");
    }

    public void RunInterstitialAds()
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.RunInterstitialAds");
    }

    public void RunVideoAds()
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.RunVideoAds");
    }

    public void ShowBanner()
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.ShowBanner");
    }

    public void HideBanner()
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.HideBanner");
    }

    public void BannerReload()
    {
    }

    public void SkipInterstitialOnce()
    {
        this.skipInterstitial = true;
    }

    public void SkipInterstitial(float seconds = 20f)
    {
        BitMango.Diagnostics.Debugger.Log("AdManager.SkipInterstitialForSeconds : " + seconds);
        this.timeOfEndSkip = Time.realtimeSinceStartup + seconds;
    }

    public bool IsPossibleToShowVideo()
    {
        return true;
    }

    public bool IsPossibleToShowInterstitial(string zone, bool isEnter)
    {
        return false;
    }

    private void CallbackNextFrame(Action<bool> callback, bool param)
    {
        if (callback != null)
        {
            TaskManager.DoNextFrame(delegate
            {
                callback(param);
            }, 1);
        }
    }


    public void ShowVideo(string zone, Action<bool> onClose = null, int level = 0)
    {
        if (!this.IsPossibleToShowVideo())
        {
            this.CallbackNextFrame(onClose, false);
            return;
        }
        Platform.SafeCallback<AdManager.AdType>(this.OnPrevShow, AdManager.AdType.VIDEO);
        this.ShowVideoAd(zone, onClose);
    }

    public void ShowInterstitial(string zone, bool isEnter, Action<bool> onClose = null)
    {
        if (!this.IsPossibleToShowInterstitial(zone, isEnter))
        {
            this.CallbackNextFrame(onClose, false);
            return;
        }
        Platform.SafeCallback<AdManager.AdType>(this.OnPrevShow, AdManager.AdType.INTERSTITIAL);
        this.VideoAdShown = false;
        this.ShowInterstitialAd(zone, onClose);
    }

    private void ShowInterstitialAd(string zone, Action<bool> innerOnClose)
    {
        if (Scene.CurrentScene() != null)
        {
            Scene.Lock(Context.BMPlatform.appDisplayName);
        }
    }

    private void ShowVideoAd(string zone, Action<bool> innerOnClose)
    {
        if (Scene.CurrentScene() != null)
        {
            Scene.Lock(Context.BMPlatform.appDisplayName);
        }
        float realtimeSinceStartup = Time.realtimeSinceStartup;
    }

    private void CreateBannerBG()
    {
    }

    public const string Version = "4.0";

    public string admobID;

    private int mode = 273;

    private bool useVideo;

    private bool useInterstitial;

    private bool useBanner;

    private static Rect bannerBgRect;

    private static GUIStyle bannerBgStyle;

    private int impressionCount;

    private bool videoAdShown;

    private bool interAdShown;

    private bool skipInterstitial;

    private float lastDisplayTime = -1000f;

    private float timeOfEndSkip;

    public enum AdType
    {
        NONE,
        INTERSTITIAL,
        BANNER,
        VIDEO
    }
}
